//
//  MoreViewController.m
//  UIWebView
//
//  Created by Hello Cai on 2017/6/19.
//  Copyright © 2017年 keisun. All rights reserved.
//

#import "MoreViewController.h"
#import "NSObject+Extension.h"

@interface MoreViewController ()<UIWebViewDelegate>

@end

@implementation MoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    webView.delegate = self;
    [self.view addSubview:webView];
    [webView loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"MoreP" withExtension:@"html"]]];
}


- (void)sendMessage:(NSString *)number number2:(NSString *)number2 number3:(NSString *)number3
{
    NSLog(@"%s", __func__);
    NSLog(@"1:%@, 2:%@, 3:%@",number,number2,number3);
}

- (void)sendMessage:(NSString *)number number2:(NSString *)number2
{
    NSLog(@"%s", __func__);
    NSLog(@"1:%@, 2:%@",number,number2);
}

- (void)sendMessage:(NSString *)number{
    NSLog(@"%s", __func__);
    NSLog(@"1:%@",number);
}

#pragma mark - <UIWebViewDelegate>
/**
 通过这个方法完成JS调用OC
 JS和OC交互的第三方框架：WebViewJavaScriptBridge
 */
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    // url == xmg://sendMessage_?200    (参数200)
    NSString *url = request.URL.absoluteString;
    NSString *scheme = @"xmg://";
    if ([url hasPrefix:scheme]) {
        
        //获得协议后面的路径 path = sendMessage_number2_?200&300 (参数200和300)
        NSString *path = [url substringFromIndex:scheme.length];
        //利用?切割路径
        NSArray *subpaths = [path componentsSeparatedByString:@"?"];
        //方法名 methodName = sendMessage:number2:
        NSString *methodName = [[subpaths firstObject] stringByReplacingOccurrencesOfString:@"_" withString:@":"];
        // 参数
        NSArray *params = nil;
        if (subpaths.count == 2) {
            params = [[subpaths lastObject] componentsSeparatedByString:@"&"];
        }
        //自己写的扩展方法
        [self performSelector:NSSelectorFromString(methodName) withObjects:params];
        return NO;
    }
    NSLog(@"想加载其他请求，不是想调用OC的方法");
    return YES;
}

@end
